/**
 * 下拉框组件
 */
Ext.ns("Ext.lams","Ext.lams.comp");
/**
 * 性别下拉框
 * 
 * @class Ext.lams.Combobox_Sex
 * @extends Ext.form.ComboBox
 */
Ext.lams.comp.ComboBox_Sex = Ext.extend(Ext.form.ComboBox, {
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});
				Ext.lams.comp.ComboBox_Sex.superclass.constructor.call(this, {
							fieldLabel : '性别',
							mode : 'local',
							editable : false,
							triggerAction : 'all',
							// value:'1',
							emptyText : "请选择",
							store : new Ext.data.JsonStore({
										fields : ['value', 'name'],
										data : [{'value':'1','name':'男'},{'value':'2','name':'女'}]
									}),
							valueField : "value",
							displayField : "name"
						});
			}
		});
Ext.lams.comp.ComboBox_H = Ext.extend(Ext.form.ComboBox, {
			url : "",
			autoLoad:false,
			params : {},
			constructor : function(_cfg) {
				if (!_cfg) {
					_cfg = {
						url : '',
						params : {}
					};
				}
				this.url = _cfg.url;
				// this.sqlCode = _cfg.sqlCode;
				this.params = _cfg.params;
				this.autoLoad = _cfg.autoLoad || false;
				Ext.apply(_cfg, {
							emptyText : "请选择",
                            mode : 'remote',
                            hiddenValue:"-1",
							valueField : "VALUE",
							displayField : "NAME"
						});
				Ext.lams.comp.ComboBox_H.superclass.constructor
						.call(this, _cfg);
				// this.addEvents("selected");
			},
			initComponent : function() {
				var me = this;
				var _store = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : me.url,
										method : "GET"
									}),
							autoLoad:me.autoLoad,
							baseParams : me.params,
							totalProperty : "totalCount",
							root : "data",
							fields : ["VALUE", "NAME"]
						});
				Ext.apply(this, {
							store : _store
						});
				Ext.lams.comp.ComboBox_H.superclass.initComponent.call(this);
				// me.on("select");
                //_store.load();
			}
		});

/**
 * 学校下拉框
 * 
 * @class Ext.lams.comp.Combobox_School
 * @extends Ext.form.ComboBox
 */
Ext.lams.comp.ComboBox_School = Ext.extend(Ext.lams.comp.ComboBox_H, {
			constructor : function(_cfg) {
				if (!_cfg)
					_cfg = {};
				Ext.apply(_cfg, {
							fieldLabel : "学校",
							editable : false,
							triggerAction : "all",
							url : "queryAction.action",
							params : {
								sqlCode : "SEL_SCHOOL001"
							}
						});
				Ext.lams.comp.ComboBox_School.superclass.constructor.call(this,
						_cfg);
			}
		});

Ext.lams.comp.ComboBox_Class = Ext.extend(Ext.lams.comp.ComboBox_H, {
			constructor : function(_cfg) {
				if (!_cfg)
					_cfg = {};
				Ext.apply(_cfg, {
							fieldLabel : "班级",
							editable : false,
							triggerAction : "all",
							url : "queryAction.action",
							params : {
								sqlCode : "SEL_CLASS001"
							}
						});
				Ext.lams.comp.ComboBox_Class.superclass.constructor.call(this,
						_cfg);
			},
			loadData : function(school_id) {
				var me = this;
				var store = me.getStore();
				me.setValue();
				store.removeAll();
				store.reload({
							params : {
								SCHOOL_ID : school_id
							}
						});
			}
		});

Ext.lams.comp.AutoComplete = Ext.extend(Ext.form.ComboBox, {
			//sqlCode : "",
            fields : [],
            params:{},
			constructor : function(_cfg) {
				if (!_cfg)
					_cfg = {};
				Ext.apply(_cfg, {
							typeAhead : false,
							loadingText : '载入中...',
							pageSize : 10,
                            hiddenValue:"-1",
							hideTrigger : true,
                            itemSelector : 'div.x-combo-list-item',
							minChars : 1
						});
				//this.sqlCode = _cfg.sqlCode;
                this.params = _cfg.params || {};
                this.fields = _cfg.fields || [{
                                        name : "VALUE"
                                    }];
				Ext.lams.comp.AutoComplete.superclass.constructor.call(this,
						_cfg);
			},
			initComponent : function() {
				var ds = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : "autoComplete.action",
										method : "GET"
									}),
							baseParams : this.params,
							totalProperty : "totalCount",
							root : "data",
							fields : this.fields
						});
				if (!this.store)
					this.store = ds;
				Ext.lams.comp.AutoComplete.superclass.initComponent.call(this);
			}
		});
Ext.lams.comp.PagingToolbar = Ext.extend(Ext.PagingToolbar, {
			grid:{},
			isExcel:true,
			constructor : function(cfg) {
				Ext.apply(this, cfg || {});
				this.grid = cfg.grid;
				this.isExcel = cfg.isExcel || true;
				Ext.lams.comp.PagingToolbar.superclass.constructor.call(this, {
							pageSize : 20,
							displayInfo : true
						});
			},
			initComponent : function() {
				var me = this;
				var excel = new Ext.Button({
							text : '',
							tooltip : "导出Excel",
							iconCls : "silk-page-excel",
							// width : 75,
							handler : this.executeExcel
						});
				var com = new Ext.form.ComboBox({
							mode : 'local',
							editable : false,
							triggerAction : 'all',
							value:20,
                            width:60,
                            autoWidth:true,
							//emptyText : "20",
							store : new Ext.data.SimpleStore({
										fields : ['name'],
										data : [[10], [20], [30], [50],
												[100],[500]]
									}),
							// valueField : "value",
							displayField : "name",
							listeners : {
								select : function(_com, value) {
									me.pageSize = value.data.name;
                                    me.store.load({params:{start:0,limit:me.pageSize}});
								}
							}
						});
				if(this.isExcel)
					Ext.apply(this,{
	                    items:['-',com,'-',excel]
	                });
	            else{
	            	Ext.apply(this,{
	                    items:['-',com]
	                });
	            }
				Ext.lams.comp.PagingToolbar.superclass.initComponent.call(this);
			},
			executeExcel : function() {
                this.store;
			}
		});
		
		/**
		 * S_CODE下拉框
		 * @class Ext.lams.comp.ComboBox_SCode
		 * @extends Ext.lams.comp.ComboBox_H
		 */
		Ext.lams.comp.ComboBox_SCode = Ext.extend(Ext.lams.comp.ComboBox_H, {
			code_cate : "",
			constructor : function(_cfg) {
				if (!_cfg)
					_cfg = {};
				Ext.apply(_cfg, {
							autoLoad:true,
							triggerAction : "all",
							url:'queryCodeCate.action',
							params:{'codecate':_cfg.code_cate}
						});
				this.code_cate = _cfg.code_cate;
				Ext.lams.comp.ComboBox_SCode.superclass.constructor.call(this,
						_cfg);
			}
//			,
//			initComponent : function() {
//				var me = this;
//				var _store = new Ext.data.JsonStore({
//							proxy : new Ext.data.HttpProxy({
//										url : "queryCodeCate.action",
//										method : "GET"
//									}),				
//
//							autoLoad:true,
//							baseParams : {'code_cate':me.code_cate},
//							totalProperty : "totalCount",
//							root : "data",
//							fields : ["VALUE", "NAME"]
//						});
//				Ext.apply(this, {
//							store : _store
//						});
//				Ext.lams.comp.ComboBox_SCode.superclass.initComponent.call(this);
//			}
		});
		
Ext.reg("pagingbar_excel",Ext.lams.comp.PagingToolbar);
Ext.reg("xauto", Ext.lams.comp.AutoComplete);
Ext.reg("combo_sex", Ext.lams.comp.ComboBox_Sex);
Ext.reg("combo_h", Ext.lams.comp.ComboBox_H);
Ext.reg("combo_school", Ext.lams.comp.ComboBox_School);
Ext.reg("combo_class", Ext.lams.comp.ComboBox_Class);
Ext.reg("combo_scode", Ext.lams.comp.ComboBox_SCode);

Ext.lams.TreeComboBox = Ext.extend(Ext.form.ComboBox, {
	constructor : function(config) {
		Ext.apply(this, config || {});
		this.treeId = Ext.id() + '-tree';
		this.maxHeight = arguments[0].maxHeight || arguments[0].height
				|| this.maxHeight;
		this.width = this.width || 200;
//		this.minWidth = arguments[0].minWidth || arguments[0].width || this.minWidth;
		this.tpl = new Ext.Template('<tpl for="."><div style="height:'
				+ this.maxHeight + 'px"><div id="' + this.treeId
				+ '"></div></div></tpl>');
		this.store = new Ext.data.SimpleStore({
					fields : [],
					data : [[]]
				});
		this.selectedClass = '';
		this.mode = 'local';
		this.triggerAction = 'all';
		this.onSelect = Ext.emptyFn;
		this.editable = false;
		this.beforeBlur = Ext.emptyFn;
		Ext.lams.TreeComboBox.superclass.constructor.call(this);
		this.addEvents('afterchange');
		this.addEvents('select');
		this.addEvents('beforeselect');
	},
	initComponent : function() {
        
		var root = new Ext.tree.AsyncTreeNode({
					text : this.rootText,
					id : this.rootId,
					expanded : true
				});
		var tree = new Ext.tree.TreePanel({
					border : false,
					root : root,
					rootVisible : false,
					autoScroll : true,
					animate:true,
					baseparams:'id',
					loader : new Ext.tree.TreeLoader({
								dataUrl : this.treeUrl,
								clearOnLoad : true
							}),
					containerScroll : false,
					listeners : {
						scope : this,
						click : function(node) {
							this.setValue(node);
							this.collapse();
						}
					}
				});
		this.tree = tree;
      
		Ext.lams.TreeComboBox.superclass.initComponent.call(this);
        
      //  this.expand();
	},
	expand : function() {
		Ext.lams.TreeComboBox.superclass.expand.call(this);
		if (this.tree.rendered) {
			return;
		}
		Ext.apply(this.tree, {
					height : this.maxHeight,
					width : (this.width - (Ext.isIE ? 3 : 0))- 2,//(this.width||158 - (Ext.isIE ? 3 : 0))- 2,
					border : false,
					autoScroll : true
				});
		if (this.tree.xtype) {
			this.tree = Ext.ComponentMgr.create(this.tree, this.tree.xtype);
		}
		this.tree.render(this.treeId);

		var root = this.tree.getRootNode();
		if (!root.isLoaded())
			root.reload();

		this.tree.on('click', function(node) {
					var selModel = this.selectNodeModel;
					var isLeaf = node.isLeaf();

					if ((node == root) && selModel != 'all') {
						return;
					} else if (selModel == 'folder' && isLeaf) {
						return;
					} else if (selModel == 'leaf' && !isLeaf) {
						return;
					}

					var oldNode = this.getNode();
					if (this.fireEvent('beforeselect', this, node, oldNode) !== false) {
						this.setValue(node);
						this.collapse();

						this.fireEvent('select', this, node, oldNode);
						(oldNode !== node) ? this.fireEvent('afterchange',
								this, node, oldNode) : '';
					}
				}, this);
	},
	setValue : function(node) {
		this.node = node;
		var text = node.text;
		this.lastSelectionText = text;
        this.setRawValue(text);
		if (this.hiddenField) {
			this.hiddenField.value = node.id;
		}
		Ext.lams.TreeComboBox.superclass.setValue.call(this, text);
		this.value = node.id;
	},
	getValue : function() {
		return typeof this.value != 'undefined' ? this.value : '';
	},

	getNode : function() {
		return this.node;
	}
});
Ext.reg("treecombobox", Ext.lams.TreeComboBox);
/**
 * 
 * 按钮组件
 * 
 */

/**
 * 统一按钮图标样式
 */
Ext.namespace("Ext.lams.buttons");
/**
 * 查询
 * @class Ext.lams.buttons.Query
 * @extends Ext.Button
 */
Ext.lams.buttons.Query = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Query.superclass.constructor.call(this,{
            text:"查询",
            width : '75',
            iconCls:"silk-zoom"
        });
    }
});
Ext.reg("btn_query",Ext.lams.buttons.Query);
/**
 * 编辑
 * @class Ext.lams.buttons.Edit
 * @extends Ext.Button
 */
Ext.lams.buttons.Edit = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Edit.superclass.constructor.call(this,{
            text:"编辑",
            width : '75',
            iconCls:"silk-user-edit"
        });
    }
});
Ext.reg("btn_edit",Ext.lams.buttons.Edit);
/**
 * 删除
 * @class Ext.lams.buttons.Delete
 * @extends Ext.Button
 */
Ext.lams.buttons.Delete = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Delete.superclass.constructor.call(this,{
            text:"删除",
            width : '75',
            iconCls:"silk-user-delete"
        });
    }
});
Ext.reg("btn_delete",Ext.lams.buttons.Delete);
/**
 * 确定
 * @class Ext.lams.buttons.Submit
 * @extends Ext.Button
 */
Ext.lams.buttons.Submit = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Submit.superclass.constructor.call(this,{
            text:"确定",
            width : '75',
            iconCls:"silk-tick"
        });
    }
});
Ext.reg("btn_submit",Ext.lams.buttons.Submit);
/**
 * 取消
 * @class Ext.lams.buttons.Cancel
 * @extends Ext.Button
 */
Ext.lams.buttons.Cancel = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Cancel.superclass.constructor.call(this,{
            text:"取消",
            width : '75',
            iconCls:"silk-cancel"
        });
    }
});
Ext.reg("btn_cancel",Ext.lams.buttons.Cancel);
/**
 * 保存
 * @class Ext.lams.buttons.Save
 * @extends Ext.Button
 */
Ext.lams.buttons.Save = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Save.superclass.constructor.call(this,{
            text:"保存",
            width : '75',
            iconCls:"silk-disk"
        });
    }
});
Ext.reg("btn_save",Ext.lams.buttons.Save);
/**
 * 添加
 * @class Ext.lams.buttons.Add
 * @extends Ext.Button
 */
Ext.lams.buttons.Add = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.Add.superclass.constructor.call(this,{
            text:"添加",
            width : '75',
            iconCls:"silk-add"
        });
    }
});
Ext.reg("btn_add",Ext.lams.buttons.Add);
/**
 * 新建
 * @class Ext.lams.buttons.New
 * @extends Ext.Button
 */
Ext.lams.buttons.New = Ext.extend(Ext.Button,{
    constructor : function(cfg){
        Ext.apply(this,cfg);
        Ext.lams.buttons.New.superclass.constructor.call(this,{
            text:"新建",
            width : '75',
            iconCls:"silk-application-add"
        });
    }
});
Ext.reg("btn_new",Ext.lams.buttons.New);

/**
 * 
 * 验证框架
 * 
 */
if (Ext.form.VTypes) {
	Ext.apply(Ext.form.VTypes, {
				"checkCode" : function(_str) {
					var flag = false;
					if (_str != "") {
						Ext.Ajax.request({
									url : 'vaildCheckCode.action',
									params : {
										'checkCode' : _str
									},
									method : 'POST',
									async : false,
									success : function(response, options) {
										var result = Ext.util.JSON
												.decode(response.responseText
														.trim());
										flag = result['success'];
									}
								});
					} else {
						flag = false;
					}
					return flag;
				},
				"checkCodeText" : "验证码错误",
				"checkCodeMask" : ''
			});
}
if (Ext.form.VTypes) {
	Ext.apply(Ext.form.VTypes, {
				"checkParameName" : function(_str) {
					var flag = false;
					if (_str != "") {
						Ext.Ajax.request({
									url : 'checkParameName.action',
									params : {
										'param.parameterName' : _str
									},
									method : 'POST',
									async : false,
									success : function(response, options) {
										var result = Ext
												.decode(response.responseText
														.trim());
										flag = result['success'];
									}
								});
					}
					return flag;
				},
				"checkParameNameText" : "参数名重复",
				"checkParameNameMask" : ''
			});
}
if(Ext.form.VTypes){
	Ext.apply(Ext.form.VTypes,{
		"checkImage":function(_str,_obj){
		//	Ext.Msg.alert(_obj.id);
			var file = document.getElementById(_obj.id+"-file");
			Ext.Msg.alert(file.fildSize);
			return false;
		},
		"checkImageText":"图片太大",
		"checkImageMask":""
	});
}
/**
 * 
 */
Ext.namespace("Utils");
Utils.getPath = function(){
		var localObj = window.location;

		var contextPath = localObj.pathname.split("/")[1];

		var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath;

		return basePath;
	};
Utils.isEnableBtn = function(btns,btn_name){
    if(btns && btns.length){
	    for(var i=0;i<btns.length;i++){
	        var btn = btns[i];
	        if(btn.BTN_NAME == btn_name)
	            return false;
	    }
    }
	return true;
};
Utils.createCookie = function(name, value, days){
	if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
};
Utils.readCookie = function(name){
	var nameEQ = name + "=",
        ca = document.cookie.split(';'),
        i,
        c,
        len = ca.length;
    for ( i = 0; i < len; i++) {
        c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
Ext.ns("H");
H.Msg = {
	minWidth:300,
	build:function(m,c){
		Ext.apply(c,{
			msg:m,
			minWidth:this.minWidth
		})
		Ext.Msg.show(c);
	},
	
	info:function(msg,fn){
		var config = {
			title:'提示',
			icon:Ext.Msg.INFO,
			buttons:Ext.Msg.OK,
			fn:fn
		}
		H.Msg.build(msg,config);
	},
	warning:function(msg,fn){
		var config = {
			title:'警告',
			icon : Ext.Msg.WARNING,
			buttons:Ext.Msg.OK,
			fn:fn
		}
		H.Msg.build(msg,config);
	},
	error:function(msg,fn){
		var config = {
			title:'错误',
			icon : Ext.Msg.ERROR,
			buttons:Ext.Msg.OK,
			fn:fn
		}
		H.Msg.build(msg,config,'error');
	},
	confirm:function(msg,fn){
		var config = {
			title:'提示',
//			confirm:true,
			icon:Ext.Msg.INFO,
			buttons:Ext.Msg.YESNO,
			fn:fn
		}
		H.Msg.build(msg,config);
	},
	prompt:function(msg,ismultiline,fn){
		var config = {
			title:'提示',
			prompt:true,
			icon:Ext.Msg.INFO,
			multiline:ismultiline || false,
			buttons:Ext.Msg.OKCANCEL,
			fn:fn
		}
		H.Msg.build(msg,config);
	}
}
